package offer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class offer45 {
}
class Solution45 {
    public static String minNumber(int[] nums) {
       if(nums.length==0) return null;
       String []strs = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            strs[i] = String.valueOf(nums[i]);
        }
        quickSort(strs,0, nums.length-1);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < strs.length ; i++) {
            sb.append(strs[i]);
        }
        String res = sb.toString();
        return res;
    }
        static void   quickSort(String[] strs, int left, int right) {
        if(left>right) return;
        int i = left;
        int j = right;
        String tmp = strs[left];
        while (i<j)
        {
            while ((strs[j]+tmp).compareTo(tmp+strs[j])>=0) j--;
            while ((strs[i]+tmp).compareTo(tmp+strs[i])<=0)i++;
            if(i<j){
                String temp = strs[i];
                strs[i] = strs[j];
                strs[j] = temp;
            }
        }
        strs[left] = strs[i];
        strs[i] = tmp;
        quickSort(strs,left,i-1);
        quickSort(strs,i+1,right);
    }

    public static void main(String[] args) {
        int []nums= {3,32,321,11,4};
        minNumber(nums);
    }
}